package com.angel.infrastructure.db.config;

import com.google.common.collect.Iterables;
import org.apache.ibatis.reflection.factory.DefaultObjectFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;

/*
 * 更新关注微信公众号： 为了offer
 * @author parkin
 * @description 解决mybatis-plus不支持返回Optional对象
 * @date 2021/11/15
 */
@Component
@Configuration
public class OptionalAwareObjectFactory extends DefaultObjectFactory {

    @SuppressWarnings("unchecked")
    @Override
    public <T> T create(Class<T> type, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {

        if (Optional.class.isAssignableFrom(type))
            return (T) Optional.ofNullable(Iterables.getOnlyElement(constructorArgs));
        else
            return super.create(type, constructorArgTypes, constructorArgs);

    }
}
